package ee.ttu.rakarh.commands;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ee.ttu.rakarh.model.data.dao.ItemDAO;
import ee.ttu.rakarh.model.data.dao.ItemDAOImpl;
import ee.ttu.rakarh.model.data.dao.ItemTypeDAO;
import ee.ttu.rakarh.model.data.dao.ItemTypeDAOImpl;
import ee.ttu.rakarh.model.data.entities.Item;
import ee.ttu.rakarh.model.data.entities.ItemAttribute;
import ee.ttu.rakarh.model.data.entities.ItemAttributeType;
import ee.ttu.rakarh.model.data.entities.ItemType;
import ee.ttu.rakarh.model.data.entities.TypeAttribute;
import ee.ttu.rakarh.model.form.ItemAttributeForm;
import ee.ttu.rakarh.model.form.ItemForm;
import ee.ttu.rakarh.services.ItemAttributeService;
import ee.ttu.rakarh.services.ItemService;
import ee.ttu.rakarh.services.ItemServiceImpl;

public class UpdateItem implements Command {
	public static final Logger LOG = Logger.getLogger(UpdateItem.class);
	@Override
	public Boolean execute(HttpServletRequest request,
			HttpServletResponse response) {
		String stringItemId = request.getParameter("itemId");
		if (stringItemId == null)
			return false;
		int itemId;
		try {
			itemId = Integer.parseInt(stringItemId);
		} catch (NumberFormatException e) {
			LOG.error(e);
			return false;
		}
		ItemForm itemForm = new ItemForm();

		itemForm.setName(request.getParameter("name"));
		itemForm.setSalePrice(request.getParameter("salePrice"));
		itemForm.setProducer(request.getParameter("producer"));
		itemForm.setDescription(request.getParameter("description"));
		itemForm.setProducerCode(request.getParameter("producerCode"));
		// itemType

		ItemAttributeService itemAttributeService = new ItemAttributeService();
		ItemDAO itemDAO = new ItemDAOImpl();
		Item itemToBeUpdated = itemDAO.getItemById(itemId);
		Collection<TypeAttribute> typeAttributes = itemAttributeService
				.getTypeAttributesByTypeId(itemToBeUpdated.getItemType().getId());

		Collection<ItemAttributeForm> itemAttributeForms = new ArrayList<ItemAttributeForm>();

		System.out.println("Attributes:");
		for (TypeAttribute typeAttribute : typeAttributes) {
			String itemAttributeValue = request.getParameter(Integer
					.toString(typeAttribute.getItemAttributeType().getId()));
			Boolean isReq = typeAttribute.isRequired();
			itemAttributeValue = itemAttributeValue == null ? ""
					: itemAttributeValue;
			ItemAttributeForm itemAttributeForm = new ItemAttributeForm();
			itemAttributeForm.setValue(itemAttributeValue);

			itemAttributeForm.setIsReq(isReq);

			ItemAttributeType itemAttributeType = new ItemAttributeType();
			itemAttributeType = typeAttribute.getItemAttributeType();

			itemAttributeForm.setItemAttributeType(itemAttributeType);

			itemAttributeForms.add(itemAttributeForm);
		}

		itemForm.setItemAttributeForms(itemAttributeForms);
		Item item = new Item();
		item.setId(itemToBeUpdated.getId());
		Boolean res = itemForm.convertToModel(item);
		System.out.println(res);
		if (res == true) {
			
			itemDAO.updateItem(item);
		} else {
			LOG.info("item is noooot valid");
		}
		

		if (!res) {
			String errorsString = itemForm.getErrors().toString();
			errorsString = errorsString.substring(1, errorsString.length() - 1);
			request.setAttribute("result", errorsString.trim());
			LOG.info("form errors" + errorsString.trim());
		} else {
			request.setAttribute("result", "");
		}
		return true;
	}

}
